/**  File: menu.cpp
 *   Author: Jaideep Balekar
 *           Department of Computer Science
 *           Texas A&M University
 *           315 - Programming Studio
 * Due Date: December 6th, 2009 @ 11:59
 * Contents: This class represents the menu screen which is the first screen to show up when the game is executed. 
 *			It gives the player three options - play, help, quit.
 */
 
#include <QWidget>
#include "Menu.h"
#include <QFont>
#include <QMessageBox>
#include <QTimer>
#include <iostream>
#include <vector>

Menu :: Menu(QGraphicsScene *scene) : QGraphicsView(scene)
{
counter = 0;
    this->resize(640,480);
    this->setBackgroundBrush(QPixmap("display.gif"));

    //create Play, Watch, Help, and Quit
    playButton  = new QPushButton (tr("Play"), this);
    watchButton = new QPushButton (tr("Watch"), this);
    helpButton  = new QPushButton (tr("Help"), this);
    quitButton  = new QPushButton (tr("Quit"), this);
    //setting fonts
    playButton->setFont (QFont("Times",18,QFont::Bold));
    watchButton->setFont(QFont("Times",18,QFont::Bold));
    helpButton->setFont (QFont("Times",18,QFont::Bold));
    quitButton->setFont (QFont("Times",18,QFont::Bold));
    //connecting buttons to actions
    QObject:: connect(playButton, SIGNAL(clicked()),this,SLOT(enterPlayState()  ));
    QObject:: connect(watchButton,SIGNAL(clicked()),this,SLOT(enterWatchState() ));
    QObject:: connect(helpButton, SIGNAL(clicked()),this,SLOT(showHelp()        ));

    //set the scene window
    scene->setSceneRect(0,0,600,480);
    scene->setFocus();

    //adding widgets to the scene
    scene->addWidget(playButton);
    scene->addWidget(watchButton);
    scene->addWidget(helpButton);
    scene->addWidget(quitButton);

    //set the Geometry of the widgvoidets
    playButton->setGeometry (225,250,200,50);
    watchButton->setGeometry(225,300,200,50);
    helpButton->setGeometry (225,350,200,50);
    quitButton->setGeometry (225,400,200,50);
    intro->play();
}

void Menu::enterPlayState()
{
    gameObject = new Game(&topLevelScene, false); //toplevelScene of type QGraphicsScene
    gameObject->show();
    this->hide();
}

void Menu::enterWatchState() {
    MapBuilder* mp = new MapBuilder();
    //Mapbuilder entry point
    std::vector<Dot*> allDots;
    std::vector<Wall*> wallblock;
    std::vector<BigDot*> powerDots;

    int i =0;
    for(QMap<QString, CoordChar>::const_iterator it = mp->powerdotmap.constBegin(); it != mp->powerdotmap.constEnd(); it++)
    {
        BigDot* tempDot = new BigDot(it.value().xcoord*WIDTH,it.value().ycoord*HEIGHT);
        powerDots.push_back(tempDot);
        topLevelScene.addItem(powerDots.at(i));
        i++;
    }
    i =0;
    for(QMap<QString, CoordChar>::const_iterator it = mp->dotmap.constBegin(); it != mp->dotmap.constEnd(); it++)
    {
        Dot* tempDot = new Dot(it.value().xcoord*WIDTH,it.value().ycoord*HEIGHT);
        allDots.push_back(tempDot);
        topLevelScene.addItem(allDots.at(i));
        i++;
    }

    i=0;
    for(QMap<QString, CoordChar>::const_iterator it = mp->map.constBegin(); it != mp->map.constEnd(); it++)
    {
        Wall* tempWall = new Wall();
        tempWall->setPos(it.value().xcoord*WIDTH,it.value().ycoord*HEIGHT);
        wallblock.push_back(tempWall);
        topLevelScene.addItem(wallblock[i]);
        i++;
    }
    std::cout << "Attempting to make view" << std::endl;
    QGraphicsView view (&topLevelScene);
    std::cout << "Attempting to show view" << std::endl;
    view.show();
    std::cout << "Attempting to hide menu" << std::endl;
    //   this->hide();
}

void Menu::showHelp()
{
    QMessageBox trybox;
    trybox.setText("Use the arrow keys to manuover Pacman around the maze\n complete the level by eating all the dots. Avoid the ghost,\n if they catch you you will lose a life. Eat the power dot(big dots),\n and the monsters will temporarily turn blue, meaning that\n you can now eat them.");
    trybox.exec();
}

